package com.chant.startFarming.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisUtils {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public boolean set(String key, Object value, long time, TimeUnit timeUnit) {
        try {
//            System.out.println("Setting key: " + key + ", value: " + value); // 添加日志
            redisTemplate.opsForValue().set(key, value, time, timeUnit);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public Object get(String key) {
//        System.out.println("Getting key: " + key); // 添加日志
        return redisTemplate.opsForValue().get(key);
    }

    public boolean expire(String key, long time, TimeUnit timeUnit) {
        try {
//            System.out.println("Expiring key: " + key); // 添加日志
            redisTemplate.expire(key, time, timeUnit);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}